package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;
    private SpecParamService specParamService;

    public SpecController(SpecGroupService specGroupService, SpecParamService specParamService) {
        this.specGroupService = specGroupService;
        this.specParamService = specParamService;
    }
    //根据分类查询规格组
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>>queryGroupByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.specGroupService.queryGroupByCategory(id));
    }
    //查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>>querySpecParams(@RequestParam(value = "categoryId",required = false)Long cid,
                                                            @RequestParam(value = "groupId",required = false)Long gid,
                                                             @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(this.specParamService.querySpecParams(cid,gid,searching));
    }
    //新增规格组
    @PostMapping("/group")
    public ResponseEntity<Void>saveGroup(@RequestBody  SpecGroupDTO groupDTO){
        specGroupService.saveGroup(groupDTO);
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //修改规格组
    @PutMapping("/group")
    public ResponseEntity<Void>updateGroup(@RequestBody SpecGroupDTO groupDTO){
        specGroupService.updateGroup(groupDTO);
        return ResponseEntity   .status(HttpStatus.NO_CONTENT).build();
    }
    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<Void>saveParam(@RequestBody SpecParamDTO body){
        specParamService.saveParam(body);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //修改规格参数
    @PutMapping("/param")
    public ResponseEntity<Void>updateParam(@RequestBody SpecParamDTO body){
        specParamService.updateParam(body);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>>querySpecList(@RequestParam("id")Long id){
        return  ResponseEntity.ok(this.specGroupService.querySpecList(id));
    }
}
